/**
 * Chrowety
 * popup/init.js
 */ 


var
bg 		= chrome.extension.getBackgroundPage,
cfg 	= bg().cfg,
twitter = bg().twitter,
tweet	= bg().tweet,
utils 	= bg().utils,
plugins = bg().plugins,

TIMELINE,
TIMELINELOAD,
TIMELINENAME,
LAST_UPDATE,
API_HITS
;

if( !cfg.isLogged() )
{
	bg().firstRun();
	window.close();
}

var DEBUG = false;

var
ui 			= window.ui || {};
ui.gestures = window.ui.gestures || {};

function init()
{
	TIMELINE = $('div#timeline');
	TIMELINELOAD = $('div#timelineLoad');
	TIMELINENAME = $('#timelineName');
	LAST_UPDATE = $('#timelineLastUpdate');
	API_HITS = $('#apiHits');
	
	i18n.init();
	ui.user.init();
	ui.timeline.init();	
	ui.gestures.painel.init();
	ui.gestures.box.init();
	ui.gestures.tweet.init();	
	
    if(DEBUG)
    {    
    	bdd.initAll('ui.gestures.box', 'ui.gestures.painel', 'ui.gestures.tweet', 'ui.specialchars', 'ui.timeline', 'ui.tweet', 'ui.user');
    }	
    
    /*
    (function(){	 
        var
        now = new Date().getTime(),
        time = new Date( now - webkitPerformance.timing.navigationStart );
        	
        utils.time.navigationStart = {
        	'seconds': time.getSeconds(),
        	'milliseconds': time.getMilliseconds(),
        	'result': time.getSeconds() + 's' + time.getMilliseconds() + 'ms' 
        };               
    });   
    
    //console.log('Performance:', utils.time.navigationStart.result );
    */
};


function showWindowPopup()
{
	if( bg().windowPopup )
	{
		return;
	}
	else
	{
		chrome.windows.create({
			url: './client.html',
			width: 576,
			height: 655,
			type: 'popup'
		}, 
		function( win )
		{
			bg().windowPopup = win.id;
		});	
	}
}

function $S(q)
{
	return document.querySelectorAll(q);
};

chrome.extension.onRequest.addListener(function( request, sender, sendResponse ){
	sendResponse({});
	switch( request.action )
	{
		case 'updateApiHits':
			ui.timeline.updateApiHits();
			break;
			
		case 'updateCounter':
			ui.timeline.updateIndividualCounter( request.type, request.name );
			break;
			
		case 'newTweets':
			//TODO: Append a message alert
			//ui.timeline.counter();
			ui.timeline.updateIndividualCounter( request.type, request.name );
			break;
			
		case 'restart':
			window.location.reload();
			break;
			
		case 'close':
			window.close();
			break;
	}
});

window.onload = init;